<template>
	<view class="container">
		<uni-forms ref="myForm" :rules="rules" :modelValue="formData" label-position="top" labelWidth="80px">
			<uni-forms-item label="原密码" required name="oldPassword">
				<uni-easyinput type='password' v-model="formData.oldPassword" placeholder="" />
			</uni-forms-item>
			<uni-forms-item label="新密码" required name="newPassword">
				<uni-easyinput type='password' v-model="formData.newPassword" placeholder="" />
			</uni-forms-item>
			<uni-forms-item label="确认新密码" required name="confirmPassword">
				<uni-easyinput type='password' v-model="formData.confirmPassword" placeholder="" />
			</uni-forms-item>
		</uni-forms>
		<button type="primary" @click="submit()">修改</button>
	</view>
</template>

<script>
	import {modifyPassword} from '@/api/user.js'
	import {setUid,getUid,setUsername,getUesrname, getUsername} from '@/utils/userInfo'
	
	export default{
		data(){
			return {
				// 基础表单数据
				formData: {
					oldPassword: '',
					newPassword: '',
					confirmPassword: '',
				},
				// 校验规则
				rules: {
					oldPassword: {
						rules: [{
							required: true,
							errorMessage: '原密码不能为空'
						},{
							minLength: 5,
							maxLength: 16,
							errorMessage: '{label}长度在 {minLength} 到 {maxLength} 个字符',
						}]
					},
					newPassword: {
						rules: [{
							required: true,
							errorMessage: '新密码不能为空'
						},{
							minLength: 5,
							maxLength: 16,
							errorMessage: '{label}长度在 {minLength} 到 {maxLength} 个字符',
						}]
					},
					confirmPassword: {
						rules: [{
							required: true,
							errorMessage: '确认新密码不能为空'
						},{
							minLength: 5,
							maxLength: 16,
							errorMessage: '{label}长度在 {minLength} 到 {maxLength} 个字符',
						}]
					}
				},
			}
		},
		methods:{
			submit(ref) {
				// console.log(this.baseFormData);
				this.$refs['myForm'].validate().then(res => {
					// console.log('success', res);
					
					if(this.formData.newPassword !== this.formData.confirmPassword){
						uni.showToast({
							title: '新密码与确认新密码不一致，请重新填写',
							icon: 'none'
						})
						return
					}
					if(this.formData.oldPassword === this.formData.confirmPassword){
						uni.showToast({
							title: '新密码不能与原密码相同，请重新填写',
							icon: 'none'
						})
						return
					}
					res["uid"] = getUid()
					console.log("res:",res)
					modifyPassword(res).then( res => {
						// console.log("login.js  sucess:",res)
						// console.log("username:",getUsername())
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						
					})
				}).catch(err => {
					console.log('err', err);
				})
			},
		}
	}
</script>

<style >
	.container {
			padding: 15px;
			background-color: #fff;
		}
</style>